//
//  FloatingWidgetViewController.m
//  FloatingWidget
//
//  Created by silent on 10. 3. 10..
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "FloatingWidgetViewController.h"

@implementation FloatingWidgetViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (void)displaySubViews:(UIView *)mainView {
	NSLog([mainView description]);
	
	NSArray *subViews = [mainView subviews];

	for (UIView *subView in subViews) {
		[self displaySubViews:subView];
	}
	
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	//
	CGRect rect = CGRectMake(10,10,300,150);
	UIWidgetViewer2 *widgetViewer = [[UIWidgetViewer2 alloc] initWithFrame:rect];
//	[widgetViewer setUserInteractionEnabled:YES];
	[self.view addSubview:widgetViewer];


	[widgetViewer setURL:@"http://m.naver.com"];

	[self displaySubViews:widgetViewer];
	
	[widgetViewer release];
	
	//
	NSLog(@"=================");
	
	//
	CGRect rect2 = CGRectMake(10,210,300,150);
	UIWidgetViewer2 *widgetViewer2 = [[UIWidgetViewer2 alloc] initWithFrame:rect2];
	widgetViewer2.scalesPageToFit = YES;
//	[widgetViewer2 setUserInteractionEnabled:YES];
	[self.view addSubview:widgetViewer2];

	

	
	
	
	
	
	
	[widgetViewer2 setURL:@"http://m.daum.net"];
	
	[self displaySubViews:widgetViewer2];
	
	[widgetViewer2 release];
	
	
	//
	/*
	CGRect rect2 = CGRectMake(120,10,100,20);
	
	UIButton *button = [[UIButton alloc] initWithFrame:rect2];

	[self.view addSubview:button];

	[button release];
	 */
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
CGPoint startLocation;

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
	// Retrieve the touch point
	CGPoint pt = [[touches anyObject] locationInView:self.view];
	startLocation = pt;
	//[[self superview] bringSubviewToFront:self];
	
	NSLog(@"touchesBegan main %3f, %3f", pt.x, pt.y);
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
	// Move relative to the original touch point
	CGPoint pt = [[touches anyObject] locationInView:self.view];
	CGRect frame = [self.view frame];
	frame.origin.x += pt.x - startLocation.x;
	frame.origin.y += pt.y - startLocation.y;
	
	startLocation = pt;
	//[self setFrame:frame];
	
	NSLog(@"touchesMoved main %3f, %3f", pt.x, pt.y);
	NSLog(@"touchesMoved main %3f, %3f", frame.origin.x, frame.origin.y);
	
	for (UIView *subView in [self.view subviews]) {
		CGRect originFrame = subView.frame;
		originFrame.origin.x += frame.origin.x;
		
		if ([subView isKindOfClass:[UIImageView class]]) {
			NSLog(@">>> %3f", originFrame.origin.x);
			if (originFrame.origin.x > 0 || originFrame.origin.x < -1590) {
				return;
			}
		}
		
		subView.frame = originFrame;
	}
}

@end
